#!/usr/bin/env bash
get_char() {
  SAVEDSTTY=$(stty -g)
  stty -echo
  stty cbreak
  dd if=/dev/tty bs=1 count=1 2>/dev/null
  stty -raw
  stty echo
  stty $SAVEDSTTY
}

echo 'install JDK1.8'
echo "Press any key to begin or Press Ctrl+C to cancel"
char=$(get_char)

# 是否为root
[[ $EUID -ne 0 ]] && echo -e "must be root" && exit 1

# 从华为云镜像下载
if [ ! -f "./jdk-8u202-linux-x64.tar.gz" ]; then
  wget https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
fi

# 解压JDK到指定文件夹
mkdir -p /usr/local/java && tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/local/java

# 添加JDK到环境变量


echo 'export JAVA_HOME=/usr/local/java/jdk1.8.0_202' >>/etc/profile
echo 'export JRE_HOME=$JAVA_HOME/jre' >> /etc/profile
echo 'export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' >>/etc/profile
echo 'export PATH=${JAVA_HOME}/bin:$PATH' >>/etc/profile


source /etc/profile

# 检测Java是否安装
java -version
if [ $? -eq 0 ]; then
  echo  "JDK1.8安装成功"
else
  echo -e "JDK1.8安装失败"
fi